home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3492 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  58 lines

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Basic Question on SWITCH
  5. Date: 29 Jan 1996 08:17:49 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4eikud$e7b@solutions.solon.com>
  8. References: <4e4cu4$95f@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4e4cu4$95f@vixen.cso.uiuc.edu>, HOTARD  <jhotard> wrote:
  12. >I am just learning how to program in C, and I had a question about switch.
  13. >I am writing a program that looks at a character and then determines if it is a
  14. >letter or number.  This program must use the switch command , can I place a
  15. >range on the case command somehow??
  16.  
  17. >ie:  Switch (var)
  18. >    case 0-9:
  19.  
  20. >or case (isdigit):  
  21.  
  22. >would anything like this work???
  23.  
  24. No.
  25.  
  26. One can only assume from "This program must use the switch command" that
  27. this is homework; especially because switch is not a "command".
  28.  
  29. I dare you to try this:
  30. main() {
  31.     int c, x, printf();
  32.     c = getchar();
  33.     switch (x = ((c = (unsigned char) c), /* try to ensure a legal char. */
  34.         ((!!isdigit(c)) << 1) | ((!!isalpha(c)) << 0)))
  35.     default:
  36.         printf(
  37.     (x == 0 ? "'\\x%x' is neither a letter nor a number.\n" :
  38.      (x == 1 ? "'%c' is a letter.\n" : (x == 2 ? "'%c' is a number.\n" :
  39.       (x == 3 ? "'%c' is both a letter and a number.\n" :
  40.         "'\\x%x' is odd.\n"))), c);
  41.     return 0;
  42. }
  43.  
  44. It uses a switch, and should correctly identify the first character of
  45. standard input.
  46.  
  47. I leave making it into a loop as an exercise for the reader.
  48.  
  49. As far as I can tell, this is legal and strictly conforming ANSI.
  50.  
  51. -s
  52. p.s.: Which is not to say it's good code.
  53. -- 
  54. Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
  55. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  56. Using trn?  Weird new newsgroup problem?  I know the fix!  Email me!
  57. The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
  58.